<?php
namespace app\admin\controller;
use think\Db;
class Banner extends Base{
    public function banner_list(){
        $list=Db::name('banner')->where('state','<',3)->order('id asc')->select();
        foreach ($list as $key => $value) {
            if($list[$key]['type']==1){
                $list[$key]['img']=Db::name('product')->where('id',$list[$key]['url'])->value('img');
                $list[$key]['title']=Db::name('product')->where('id',$list[$key]['url'])->value('title');
            }
        }
        $count=Db::name('banner')->where('state','<',3)->count();
        $this->assign('list',$list);
        $this->assign('count',$count);
        return $this->fetch();
    }
    public function banner_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['url']=$param['url'];
            $data['intro']=$param['intro'];
            if($param['type']==1){
                $product=Db::name('product')->where('id',$param['url'])->where('state',1)->find();
                if ($product) {
                    $data['type']=$param['type'];
                    $data['state']=1;
                    $data['time']=time();
                    $data['uptime']=time();
                    $res=Db::name('banner')->insert($data);
                    if($res){
                        return json(['code' => 200,'msg' => '成功']);
                    }else{
                        return json(['code' => 500,'msg' => '失败']);
                    }
                }else{
                    return json(['code' => 501,'msg' => '体检套餐不存在或未启用']);
                }
            }else{
                $data['title']=$param['title'];
                $data['img']=$param['img'];
                $data['type']=$param['type'];
                $data['state']=1;
                $data['time']=time();
                $data['uptime']=time();
                $res=Db::name('banner')->insert($data);
                if($res){
                    return json(['code' => 200,'msg' => '成功']);
                }else{
                    return json(['code' => 500,'msg' => '失败']);
                }
            }
        }
    }
    public function banner_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('banner')->where('id',$param['id'])->find();
            if ($data['type']==1) {
                $data['img']=Db::name('product')->where('id',$data['url'])->value('img');
            }
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['url']=$param['url'];
            $data['intro']=$param['intro'];
            if($param['type']==1){
                $product=Db::name('product')->where('id',$param['url'])->where('state',1)->find();
                if ($product) {
                    $data['type']=$param['type'];
                    $data['state']=1;
                    $data['time']=time();
                    $data['uptime']=time();
                    $res= Db::name('banner')->where(array('id'=>$param['id']))->update($data);
                    if($res){
                        return json(['code' => 200,'msg' => '成功']);
                    }else{
                        return json(['code' => 500,'msg' => '失败']);
                    }
                }else{
                    return json(['code' => 501,'msg' => '体检套餐不存在或未启用']);
                }
            }else{
                $data['title']=$param['title'];
                $data['img']=$param['img'];
                $data['type']=$param['type'];
                $data['state']=1;
                $data['time']=time();
                $data['uptime']=time();
                $res= Db::name('banner')->where(array('id'=>$param['id']))->update($data);
                if($res){
                    return json(['code' => 200,'msg' => '成功']);
                }else{
                    return json(['code' => 500,'msg' => '失败']);
                }
            }
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('banner')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('banner')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function banner_del(){
        $param = input('post.');
        $res= Db::name('banner')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
}
